package x;


// this will include all events
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.net.URL;
import java.awt.*;
import javax.swing.*;

import javax.imageio.ImageIO;


public class Game extends Canvas implements KeyListener {

        //testing these sprite images, this will not remain
        private Image sprite1;
        private Image sprite2;
        private Image sprite3;


        private BufferStrategy strategy;

        // keyboard movement
        private boolean left;
        private boolean right;
        private boolean up;
        private boolean down;

        private Map map;
        private Entity player;
        
        Image[] sprites=new Image[3];
        //declaring array for sprites

        //sprite array, in the testing phase


        // are game loop so far
        public Game() {
                //loading are sprites within looop
                try {
                        URL url = Thread.currentThread().getContextClassLoader().getResource("sprite.gif");

                        URL url2= Thread.currentThread().getContextClassLoader().getResource("sprite2.gif");

                        URL url3= Thread.currentThread().getContextClassLoader().getResource("sprite3.gif");


                        // check for null value of images within the same file as our groups source code
                        if (url == null){
                                System.err.println("Unable to find graphics");
                                System.exit(0);
                        }
                        sprite1 = ImageIO.read(url);

                        if(url2 == null){
                                System.err.println("Unable to find graphics");
                                System.exit(0);
                        }
                        sprite2= ImageIO.read(url2);

                        if(url3 == null){
                                System.out.print("Unable to find graphics");
                        }
                        sprite3= ImageIO.read(url3);

                        //position elements
                        sprites[0]= sprite1;
                        sprites[1]= sprite2;
                        sprites[2]= sprite3;







                } catch (IOException e) {
                        System.err.println("Unable to find graphics");
                        System.exit(0);
                }

                // implementing frame.
                Frame frame = new Frame("game");
                frame.setLayout(null);
                setBounds(5,5,845,680);
                frame.add(this);
                frame.setSize(845,680);
                frame.setResizable(false);

                // exit game source
                frame.addWindowListener(new WindowAdapter() {
            
                        public void windowClosing(WindowEvent e) {
                                System.exit(0);
                        }
                });

                // this will listen for keys
                frame.addKeyListener(this);
                addKeyListener(this);

                frame.setVisible(true);

                createBufferStrategy(2);
                strategy = getBufferStrategy();

                // map
                map = new Map();
                player = new Entity(sprite1, map, 1.5f, 1.5f);

                // start the game loop for now
                gameLoop();
        }

        //timing and our collison so far
        public void gameLoop() {
                boolean gameRunning = true;
                long last = System.nanoTime();


                while (gameRunning) {
                        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();


                        g.setColor(Color.white);
                        g.fillRect(0,0,840,840);

                        // rendering our game objects
                        g.translate(100,100);
                        map.paint(g);
                        player.paint(g);


                        g.dispose();
                        strategy.show();

                        // system pausing
                        try { Thread.sleep(4); } catch (Exception e) {};

                        long delta = (System.nanoTime() - last) / 1000000;
                        last = System.nanoTime();

                        //update time
                        for (int i=0;i<delta / 5;i++) {
                                logic(5);
                        }
                        //update again if needed
                        if ((delta % 5) != 0) {
                                logic(delta % 5);
                        }
                }
        }

        public void logic(long delta) {
                // check the keyboard and record which way the player
                // is trying to move this loop

                float dx = 0;
                float dy = 0;
                if (left) {
                        dx -= 1;
                }
                if (right) {
                        dx += 1;
                }
                if (up) {
                        dy -= 1;
                }
                if (down) {
                        dy += 1;
                }

                // if the player needs to move attempt to move the entity
                // based on the keys multiplied by the amount of time thats
                // passed
                if ((dx != 0) || (dy != 0)) {
                        player.move(dx * delta * 0.003f,
                                                dy * delta * 0.003f);
                }
        }

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
                // check the keyboard and record which keys are pressed
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                        left = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                        right = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                        down = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                        up = true;
                }
        }


        public void keyReleased(KeyEvent e) {
                // check the keyboard and record which keys are released
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                        left = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                        right = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                        down = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                        up = false;
                }
        }








        public static void main(String[] argv) {
               // new SplashScreen(5000);
                new Game();
        }





}

